package com.org.buildcycle2;

import android.graphics.Canvas;
import android.util.Log;
import android.view.SurfaceHolder;

public class GameThread extends Thread{
	
	private static final String TAG = GameThread.class.getSimpleName();
	
	private boolean run_bool;
	private SurfaceHolder holder;
	private GamePanel panel;
	
	// holds game state
	public void setRun(boolean run_bool){
		this.run_bool = run_bool;
	}
	
	public GameThread(SurfaceHolder holder, GamePanel panel){
		super();
		this.holder = holder;
		this.panel = panel;
	}
	
	// updates game state, renders state to screen
	@Override
	public void run(){
		
		Canvas canvas;
		Log.d(TAG, "Loop Start"); // used for logging
		
		while(run_bool){
			
			canvas = null;
			try{
				
				canvas = this.holder.lockCanvas();
				synchronized (holder){
					this.panel.onDraw(canvas); // pass canvas to onDraw
				}
				
			}finally{ // if there is an exception
				if(canvas != null){
					holder.unlockCanvasAndPost(canvas);
				}
			}
		}
	}
}
